package edu.cuit.avatar.collection;

import java.util.Objects;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/10 8:55
 */
public class Circle extends Geometric implements Cloneable, Comparable<Circle> {
    public static final float PI = 3.14f;
    private float radius;

    public Circle(float radius) {
        this.radius = radius;
    }
    public Circle(float radius, String color, boolean filled) {
        super(color, filled);
        this.radius = radius;
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    /**
     * 计算面积
     * @return 面积
     */
    @Override
    public float getArea(){
        return PI * radius * radius;
    }

    /**
     * 计算周长
     * @return 周长
     */
    @Override
    public float getPerimeter(){
        return 2 * PI * radius;
    }

    @Override
    public Circle clone() throws CloneNotSupportedException {
        return (Circle)super.clone();
    }

    @Override
    public int compareTo(Circle o) {
        return Float.compare(radius, o.radius);
    }


    /**
     * 重写equals:如果两个圆的半径相等，两个圆对象相等
     */
    @Override
    public boolean equals(Object o) {
        System.err.println("hashcode碰撞的情况下会调用equals方法比较对象是否相等");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Circle circle = (Circle) o;
        return Float.compare(radius, circle.radius) == 0;
    }

    /**
     * 重写equals方法,需要基于相同的规则重写hashCode方法
     * JDK规定:如果两个对象相等,那么它们的hashcode值一定相等
     * @return
     */
    @Override
    public int hashCode() {
        System.out.println("计算该对象的hashcode值:" + Objects.hashCode(radius));
        return Objects.hashCode(radius);
    }
}
